/*
 * Copyright (c) 2018-2023 北京华恒盛世科技有限公司
 * QUICKPOOL is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
*/

#include "subcmd.h"

static int get_fileparams(char *[], bool_t, char *, char *, char *, char *);
static void copyfile_usage(const char *);
  
/**
 * @brief       qp file rcp handler, to copy file from one node to another.
 *
 * @param[in]   argc          #1: number of input argument
 * @param[in]   argv          #2: string array of the input argument
 *
 * @retval      0             #1: succeed
 * @retval      1             #2: input option is wrong or kind of input argument not specified
 * @retval      2             #3: argument has syntax error
 * @retval      3             #4: failed to do the copy
 */
int
cmd_file_copy(int argc, char **argv)
{
    bool_t appendOpt=FALSE;
    while (TRUE) {
        int option_index = 0;
        static struct option long_options[] = {
            {"help", no_argument, 0, 'h'},
            {"append", no_argument, 0, 'a'},
            {NULL, 0, 0, 0}
        };

        int opt;
        opt = getopt_long(argc, argv, "ha", long_options, &option_index);
        if (-1 == opt) {
            break;
        }

        switch (opt) {
        case 'a':
            appendOpt = TRUE; // append content to a file.
            break;
        case 'h':
            copyfile_usage(argv[0]);
            exit(0);
        case '?':
        default:
            copyfile_usage(argv[0]);
            exit(1);
	}
    }

    char hostName[MAXNAMELEN+1];
    char userName[MAXNAMELEN+1];
    char lFile[PATH_MAX+1];
    char rFile[PATH_MAX+1];

    int option;
    if (argc == 4) {
        option = get_fileparams(argv, appendOpt, hostName, userName, lFile, rFile);
    } else if (argc == 3) {
        option = get_fileparams(argv, FALSE, hostName, userName, lFile, rFile);
    } else {
        copyfile_usage(argv[0]);
        exit(2);
    }

    if (option < 0) { // syntax error
        copyfile_usage(argv[0]);
        exit(2);
    }

    int ret;
    ret = client_copy_file(option, hostName, userName, lFile, rFile, NULL);
    if (0 > ret) {
        exit(3);
    }

    exit(0);
} // end function cmd_file_copy

/* ------------------------------------------------
 *
 *  below are static functions used in this file
 *
 * ------------------------------------------------ */

static int 
get_fileparams(char *argv[], bool_t append, char *hostname, char *username,
               char *lfile, char *rfile)
{
    char *host = NULL, *user = NULL;

    if (append) {
        if ((user = strstr(argv[2], "@")) != NULL && (host = strstr(argv[2], ":")) != NULL) {
            snprintf(username, user - argv[2] + 1, "%s", argv[2]);
            snprintf(hostname, host - user, "%s", user + 1);   
            snprintf(lfile, PATH_MAX, "%s", host + 1);
            snprintf(rfile, PATH_MAX, "%s", argv[3]);            

            return XF_OP_SUB2EXEC_APPEND;
        } else if ((user = strstr(argv[3], "@")) != NULL && (host = strstr(argv[3], ":")) != NULL) {
            snprintf(username, user - argv[3] + 1, "%s", argv[3]);
            snprintf(hostname, host - user, "%s", user + 1);
            snprintf(rfile, PATH_MAX, "%s", host + 1);
            snprintf(lfile, PATH_MAX, "%s", argv[2]);            

            return XF_OP_EXEC2SUB_APPEND;
        }
    } else {
        if ((user = strstr(argv[1], "@")) != NULL && (host = strstr(argv[1], ":")) != NULL) {
            snprintf(username, user - argv[1] + 1, "%s", argv[1]);
            snprintf(hostname, host - user, "%s", user + 1);
            snprintf(lfile, PATH_MAX, "%s", host + 1);
            snprintf(rfile, PATH_MAX, "%s", argv[2]);

            return XF_OP_SUB2EXEC;
        } else if ((user = strstr(argv[2], "@")) != NULL && (host = strstr(argv[2], ":")) != NULL) {
            snprintf(username, user - argv[2] + 1, "%s", argv[2]);
            snprintf(hostname, host - user, "%s", user + 1);
            snprintf(rfile, PATH_MAX, "%s", host + 1);
            snprintf(lfile, PATH_MAX, "%s", argv[1]);            

            return XF_OP_EXEC2SUB;
        }
    }

    return -1;
} // end function get_fileparams

static void
copyfile_usage(const char *cmd)
{
    fprintf(stdout, "qp file %s usage: ", cmd);
    fprintf(stdout, "[-h, --help] [-a, --append] file1 file2\n");

    return;
}
